import {useToast} from 'vue-toast-notification';
import 'vue-toast-notification/dist/theme-sugar.css';

const $toast = useToast();//消息提示栏
/**
 * 检查是否存
 * @param str 字符串
 * @param key 在localstore里面的key的值的列表
 * @param keyColumn 列表的某个匹配字段
 */
export const checkIsInStore = (str, key, keyColumn) => {
    let arr = JSON.parse(localStorage.getItem(key))
    let flag = false
    arr.forEach(el => {
        if (el[keyColumn] === str) {
            flag = true
        }
    })
    return flag
}

/**
 * 返回判断之后的数据，比对本地的locals tore
 * @param arr
 */
export const parseIngredient = (arr) => {
    let Arr = []
    if (localStorage.getItem("Ingredient-data-list")) {
        Arr = JSON.parse(localStorage.getItem("Ingredient-data-list"))
    }
    for (let i = 0; i < arr.length; i++) {
        let flag = false
        Arr.forEach(el => {
            if (arr[i].name === el.name) {
                flag = true
            }
        })
        arr[i].isAdd = flag
    }
    return arr;
}
/**
 * 根据id来删除
 * @param id
 */
export const removeIngredientById = async(id) => {
    let Arr = []
    if (localStorage.getItem("Ingredient-data-list")) {
        Arr = JSON.parse(localStorage.getItem("Ingredient-data-list"))
    }
    let index = 0
    for (let i = 0; i < Arr.length; i++) {
        if(Arr[i].ingredientId == id){
            index = i
        }
    }
    Arr.splice(index,1)
    localStorage.setItem("Ingredient-data-list",JSON.stringify(Arr))
}

/**
 *
 * @param info
 *
 *   buyList: [
 *     // {name: "香水香水香水香水1",
 *     // count: 999,
 *     // price: 99999.88,
 *     // id: 1}
 *   ],
 *   buyCount: 0
 */
//info:{name:"",count:0,price:0,id:0}
export const addNewPerfumeInCart = (info) => {
    if (localStorage.getItem("shopCart-data-list")) {
        try {
            let cartList = JSON.parse(localStorage.getItem("shopCart-data-list"))
            let flag = false
            cartList.forEach(el => {
                if (el.id === info.id) {
                    flag = true
                }
            })
            if (!flag) {
                cartList.push(info)
                $toast.open({
                    message:"香水"+info.name+"添加成功",
                    type:"success",
                    position:"bottom-right"
                })
            }else{
                $toast.open({
                    message:"香水"+info.name+"已添加在购物车",
                    type:"warning",
                    position:"bottom-right"
                })
            }
            localStorage.setItem("shopCart-data-list", JSON.stringify(cartList))
        } catch (e) {
            localStorage.removeItem("shopCart-data-list")
            let arr = [info]
            $toast.open({
                message:"香水"+info.name+"添加成功",
                type:"success",
                position:"bottom-right"
            })
            localStorage.setItem("shopCart-data-list", JSON.stringify(arr))
        }
    } else {
        let arr = [info]
        $toast.open({
            message:"香水"+info.name+"添加成功",
            type:"success",
            position:"bottom-right"
        })
        localStorage.setItem("shopCart-data-list", JSON.stringify(arr))
    }
}
export const remotePerfumeInCart = (id) => {
    if (localStorage.getItem("shopCart-data-list")) {
        let cartList = JSON.parse(localStorage.getItem("shopCart-data-list"))
        let index = -1
        for (let i = 0; i < cartList.length; i++) {
            if (cartList[i].id == id) {
                index = id
            }
        }
        if (index != -1) {
            cartList.splice(index, 1)
        }
    }
}